package com.avecsys.quotemanager.utils;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.avecsys.quotemanager.dao.common.EnumActor;
import com.avecsys.quotemanager.dao.common.EnumTransactionStatus;
import com.avecsys.quotemanager.mbeans.core.MessageBoardMBean;
import com.avecsys.quotemanager.mbeans.supplier.SupplierMasterMBean;

/**
 * @author Sam Sundar K
 * 
 */
public class MessageBoardUtil {

	private static final Log log = LogFactory.getLog(MessageBoardUtil.class);

	/**
	 * 
	 * @param transactionStatus
	 * @param actor
	 */
	public static void publishTransactionStatusMessage(
			TransactionStatus transactionStatus, EnumActor actor,
			String messageBoardId, String msgKey, Object msgParamValues[]) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		ResourceBundle bundle = getResourceBundle(facesContext, "tnxMsgs");
		String formatedMessage = getFormattedMessage(transactionStatus, actor,
				msgParamValues, bundle);
		MessageBoardMBean messageBoardMBean = (MessageBoardMBean) facesContext
				.getApplication().evaluateExpressionGet(facesContext,
						"#{messageBoardMBean}", MessageBoardMBean.class);
		messageBoardMBean.reset();
		if (transactionStatus.getStatus().intValue() == EnumTransactionStatus.SUCESS
				.ordinal()) {
			messageBoardMBean.getInfoList().add(formatedMessage);
		} else if (transactionStatus.getStatus().intValue() == EnumTransactionStatus.FAIL
				.ordinal()
				|| transactionStatus.getStatus().intValue() == EnumTransactionStatus.OPTIMIST_LOCK_FAIL
						.ordinal()) {
			messageBoardMBean.getErrorList().add(formatedMessage);
		} else {
			messageBoardMBean.getWarnList().add(formatedMessage);
		}
	}

	/**
	 * @param transactionStatus
	 * @param actor
	 * @param msgParamValues
	 * @param bundle
	 * @return
	 */
	private static String getFormattedMessage(
			TransactionStatus transactionStatus, EnumActor actor,
			Object[] msgParamValues, ResourceBundle bundle) {
		String paramActorValue = bundle.getString(actor.getActor());
		String message = bundle.getString("TNX_STATUS_"
				+ transactionStatus.getStatus());
		Object messageParams[] = new Object[msgParamValues.length + 1];
		messageParams[0] = paramActorValue;
		for (int i = 0; i < msgParamValues.length; i++) {
			messageParams[i + 1] = msgParamValues[i];
		}
		MessageFormat messageFormat = new MessageFormat(message);
		String formatedMessage = messageFormat.format(messageParams);
		return formatedMessage;
	}

	public static ResourceBundle getResourceBundle(FacesContext facesContext,
			String bundleName) {
		ResourceBundle resourceBundle = facesContext.getApplication()
				.getResourceBundle(facesContext, bundleName);
		return resourceBundle;
	}

	/**
	 * Static Method to reset all the messages in message board
	 */
	public static void resetMessageBoard() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		MessageBoardMBean messageBoardMBean = (MessageBoardMBean) facesContext
				.getApplication().evaluateExpressionGet(facesContext,
						"#{messageBoardMBean}", MessageBoardMBean.class);
		messageBoardMBean.reset();
	}

	public static String getValue(FacesContext facesContext, String bundleName,
			String key, Object args[]) {
		ResourceBundle resourceBundle = getResourceBundle(facesContext,
				bundleName);
		String messageKey = "#" + key + "#";
		try {
			messageKey = resourceBundle.getString(key);
		} catch (MissingResourceException mre) {
			log.error(mre);
		}
		return messageKey;
	}
}
